//
//  cityPickerDataSource.swift
//  万才信息App
//
//  Created by 陌上互联  on 16/7/18.
//  Copyright © 2016年 万才信息. All rights reserved.
//

import UIKit

class cityPickerDataSourceAndDelegate:NSObject,UIPickerViewDataSource,UIPickerViewDelegate{
    
    var provinceData=[(Int,String)]()
    var cityData=[(Int,String)]()
    
    
    override init() {
        super.init()
        for (key,value) in enumUtil.PROVINCE{
            provinceData.append((key,value))
        }
//        获取第一个省的数据
        let selectedProvince=provinceData[0].0
        cityData.removeAll()
        
        let num=enumUtil.PROVINCE_CITY_IDS[selectedProvince]?.count
        
        if(num>=1){
            for index in 0...num!-1{
                let key=enumUtil.PROVINCE_CITY_IDS[selectedProvince]![index]
                let value=enumUtil.CITY[key]
                print("遍历"+String(key)+"中")
                cityData.append((key,value!))
            }
        }
    
//        print("测试province")
//        print(provinceData)
    }
    
    
    //    列的数量
    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
        return 2
    }
    
    //    每列的行的数量
    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        switch component {
        case 0:
            return provinceData.count
        default:
            return cityData.count
        }
    }
    
    //    返回对应的字符串
    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        switch component {
        case 0:
            return provinceData[row].1
        case 1:
            return cityData[row].1
        default:
            break
        }
        return ""
    }
    
    //    选中某个行或列的时候调用
    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        if(component==0){
            let selectedProvince=provinceData[row].0
            cityData.removeAll()
            
            let num=enumUtil.PROVINCE_CITY_IDS[selectedProvince]?.count
            
            if(num>=1){
                for index in 0...num!-1{
                    let key=enumUtil.PROVINCE_CITY_IDS[selectedProvince]![index]
                    let value=enumUtil.CITY[key]
                    print("遍历"+String(key)+"中")
                    cityData.append((key,value!))
                }
            }
            
            pickerView.reloadComponent(1)
        }
    }

}